///// Replication do-file for "Gren & Leuschner 2024" /////

/// The following analyses were carried out using Stata/SE 18.0
/// This script reproduces the results in the Main text & Appendices of
/// the paper "Outcome isn't Everything: Electoral Consequences 
/// of Implementing or Withdrawing Unpopular Policies"

set more off
clear

// The script uses the user-written programs listed below. 
// Uncomment the lines to install the programs if needed.

// ssc install eventdd
// ssc install matsort
// ssc install fillmissing
// ssc install estout 
// ssc install coefplot
// ssc install ftools
// ssc install reghdfe
// net install gr0002_3, from(http://www.stata-journal.com/software/sj4-3)

// Set graph scheme
set scheme lean1


/// Defining some global macros and setting the working directory.
/// Note: The directory folder need to include the a folder named "Outputs".

global path "YOUR_FOLDER_PATH_HERE"

cd "$path"

global path_output "$path/Outputs"

/// Load the data
use "Data_Main_Text.dta", clear

/// Note that the data is a panel of Swedish precincts across the election years 2002, 2006, 2010, 2014 and 2018
/// For information om how the panel is created, see the paper.  

/// Data Preparations ///

// Generate indicator for when a school in a district is closed, the decision is withdrawn and for the period in
// between proposal and decision.

// Indicator school closure
gen closed=0 
replace closed=1 if year_left>=decision_closed & any_closed==1

// Indicator proposal withdrawal
gen reversed_alt=0 
replace reversed_alt=1 if any_closed==0 & t_post_t==1
bysort district_id: egen any_reversed_alt=max(reversed_alt)

// Indicator for period between proposal and closure.
gen post_t_pre_closed=0
replace post_t_pre_closed=1 if t_post_t==1 & any_closed==1 & closed==0

// Xtset the data
xtset district_id year_left

// Generate a variable that counts number of observation for each precinct. 
egen N_observations = count(year_left), by (district_id)

// Generate a variable that indicate if a district is observed for at least one election
// prior to a proposal of a school closure is made. 

bysort district_id: egen any_untreat= min(t_post_t)
replace any_untreat=1-abs(any_untreat)

// Generate a time variable that counts the year as 1 to 5 and one that counts -5 to -1
recode year_left  (2002=1) (2006=2) (2010=3) (2014=4) (2018=5), gen(time)
gen neg_time =-time


//Generate indicators for year of treatment
bysort district_id (time): gen proposal_periods = sum(t_post_t) 
bysort district_id (time): gen proposal_periods_pre_closed = sum(post_t_pre_closed)
replace proposal_periods_pre_closed = 0 if post_t_pre_closed==0
bysort district_id (time): gen closed_periods = sum(closed) 
bysort district_id (time): gen reversal_periods = sum(reversed_alt) 

bysort district_id (time): gen proposal_periods_max3 = min(sum(t_post_t),3) 
bysort district_id (time): gen proposal_periods_pre_closed_max3 = min(sum(post_t_pre_closed),3)
replace proposal_periods_pre_closed_max3=0 if post_t_pre_closed==0
bysort district_id (time): gen closed_periods_max3 = min(sum(closed),3)
bysort district_id (time): gen reversal_periods_max3 = min(sum(reversed_alt),3) 

// Generate pre_treatment indicator (for proposal)
bysort district_id (neg_time): gen pre_treat_periods=-sum(anyt) if  t_post_t==0 & anyt==1

// Combine before and after proposal indicators
gen event_var= pre_treat_periods if pre_treat_periods!=. 
replace event_var=proposal_periods-1 if proposal_periods!=0

// Generate indicator for cases where a) the incumbent has not changed between proposal and closure
// and b) where no changes in incumbent party has happened after the proposal
gen same_party=0
replace same_party=1 if mayoralparty==incumbent_t

bysort district_id (time): gen byte first = sum(closed) == 1  & sum(closed[_n - 1]) == 0  
gen consistent_party=0 
replace consistent_party = 1 if same_party==1 & first==1
replace consistent_party=1 if reversed_alt==1

bysort district_id: egen consistent_party_panel_closed=max(consistent_party)


gen same_party_temp=same_party if t_post_t==1
replace same_party_temp=1 if t_post_t==0
bysort district_id: egen consistent_party_panel_post_t=min(same_party_temp)

// Generate indicator if the party made the proposal still was incumbent at closure.
gen closed_same = 0 
gen closed_different=0
replace closed_same=1 if closed==1 & consistent_party_panel_closed==1
replace closed_different=1 if closed==1 & consistent_party_panel_closed==0

// Generate variable for changes in number of eligible voters in the precinct.
bysort district_id (time): gen pop_change=(tot-tot[_n-1])/(tot[_n-1])
bysort district_id (time): egen max_pop_change=max(abs(pop_change))

// Generate main outcome variable

// Vote share for the party incumbent at proposal
gen share_incum=.
// Vote share for the same party in the rest of the municipality (excluding the affected precinct.)
gen share_incum_m_prime=.
// Difference between share_incum and share_incum_m_prime
gen diff_share_incum=.
// Difference between number of eligible votes in the affected precincts and the rest of the same municipality.
gen diff_giltiga=.

// loop over different incumbent parties.
foreach k in "C" "FP" "KD" "M" "MP" "S" "V"{
local j =lower("`k'")
// I use the share all valid votes
replace share_incum=`j'/giltiga if incumbent_t=="`k'"
replace diff_share_incum = mun_sum_`j'-`j' if incumbent_t=="`k'"
replace diff_giltiga = mun_sum_giltiga-giltiga if incumbent_t=="`k'"

replace share_incum_m_prime=diff_share_incum/diff_giltiga if incumbent_t=="`k'"
}

// Take the difference in vote shares between affected precincts and the rest of the municipality for
// all elections before and after the proposal. 

gen outcome=.
replace outcome=share_incum-share_incum_m_prime

// Multiple by 100 to express in percentage points
replace outcome=outcome*100

// Generate numeric party indicator
encode incumbent_t, gen(t_party) 

// Drop cases where proposal is coded after closure
drop if decision_closed<prop_year & decision_closed!=0
// Drop cases where "Other" party was in power when the proposal was made
drop if incumbent_t=="Other"
// Drop two school proposals that are coded as closed but without any year of decision or implementation
drop if faktisknedl=="ja" & any_reversed_alt==1

// Label variables
label variable t_post_t "Proposal (all outcomes)"
label variable post_t_pre_closed "Proposal (before decision)"
label variable closed "Decision to close"
label variable reversed_alt "Proposal withdrawn"

label define proposal_periods_max3 1 "Proposal (all outcomes), first election" 2 "Proposal (all outcomes), second election" 3 "Proposal (all outcomes), third or later elections"
label define proposal_periods_pre_closed_max3 1 "Proposal (before decision), first election" 2 "Proposal (before decision), second election" 3 "Proposal (before decision), third or later elections"
label define closed_periods_max3 1 "Decision to close, first election" 2 "Decision to close, second election" 3 "Decision to close, third or later elections"
label define reversal_periods_max3 1 "Proposal withdrawn, first election" 2 "Proposal withdrawn, second election" 3 "Proposal withdrawn, third or later elections"

label values proposal_periods_max3 proposal_periods_max3
label values proposal_periods_pre_closed_max3 proposal_periods_pre_closed_max3
label values closed_periods_max3 closed_periods_max3 
label values reversal_periods_max3 reversal_periods_max3 

// Merge the data with aggregated information at precinct levels
gen long VD=vd_this
gen year=year_left
merge 1:1 VD year  using "Demographic_info.dta" ,  gen(merge_info)

/// Figure 1 ///
preserve
drop if N_observations<3

// Create global for dashed lines indicting elections
foreach x in 2002 2006 2010 2014 2018 {
	global dash`x' (scatteri  0 `x'.2 86 `x'.2, lwidth(thin) lpattern(dash) lcolor(black) c(l) m(i)
}

// Get observations in main estimation sample:
quietly xtreg outcome t_post_t if anyt==1, fe vce(cl district_id)

// Histogram
graph twoway (hist prop_year if e(sample)==1 & t==1, discrete start(2000) frequency xlabel(2002(4)2018) fcolor(gs15) ylabel(,nogrid) xlabel(,nogrid)) (hist prop_year if e(sample)==1 & t==1 & any_closed==1, discrete start(2000) frequency xlabel(2002(4)2018) fcolor(gs9) ylabel(,nogrid) xlabel(,nogrid)) $dash2002) $dash2006) $dash2010) $dash2014) $dash2018  legend(position(6) order(- "Proposals made in each year that will be:"2 "Implemented" 1 "Withdrawn" 3 "Time of local elections")) ytitle(Number of proposals) xtitle(Year) ylabel(0(20)80) yscale(range(0, 90)) plotregion(margin(zero))name("Figure1", replace))
graph export "$path_output\Figure1.eps", as(eps) name("Figure1") replace

restore

/// Analysis ///

// Define some global macros
global cond1 anyt==1, fe vce(cl district_id)
global cond3 N_observations>2 & anyt==1, fe vce(cl district_id)
global cond5 N_observations==5 & anyt==1, fe vce(cl district_id)
global cond3_untreat N_observations>2 & anyt==1 & any_untreat==1, fe vce(cl district_id)
global xt_out xtreg outcome

/// Table 1 ///
eventdd outcome if anyt==1 & N_observations>2, timevar(event_var) method(fe, cluster(district_id)) accum leads(3) lags(2) graph_op(xtitle("Time relative to proposal"))
estadd local fixed "YES" , replace
esttab using "$path_output\Table1.tex",  coeflabels(lead3 "Periods $\leq$ -3" lead2 "Period=-2" lag0 "Period=0" lag1 "Period=1" lag2 "Periods $\geq$ 2") se  b(3) se(3) varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Event study, before and after proposal") mtitles("\shortstack{(1)}") nonumbers nobase nocons s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{2}{p{0.8\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Time period 0 refer to the first election after a proposal is made and the election at time period -1 is used as baseline. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-event-study-pre-trend} \end{table}")
    eststo clear

/// Table 2 ///
eststo clear
eststo: $xt_out t_post_t if $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/Table2.tex", nofloat  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap nocons replace star(* 0.10 ** 0.05 *** 0.01) title("Main results") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \end{tabular} \label{tab:main-fe-results} \begin{tablenotes} \footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\) \end{tablenotes} }")
    eststo clear

// Test referenced in footnote 21 
test closed=reversed_alt
testparm closed reversed_alt, equal

/// Figure 3 and Table B3 ///
eststo: $xt_out i.proposal_periods_max3 if $cond3
estadd local fixed "YES" , replace
eststo: $xt_out i.proposal_periods_pre_closed_max3 i.closed_periods_max3 i.reversal_periods_max3 if $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/TableB3.tex",  label se  b(3) se(3) varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Separating effect for post-treatment periods") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase nocons s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.85\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-time-heterogenity} \end{table}")
    eststo clear

label define proposal_periods_max3 1 "1st election" 2 "2nd election" 3 "{&ge} 3rd elections", replace
label define proposal_periods_pre_closed_max3 1 "1st election" 2 "2nd election" 3 "{&ge} 3rd elections", replace
label define closed_periods_max3 1 "1st election" 2 "2nd election" 3 "{&ge} 3rd elections", replace
label define reversal_periods_max3 1 "1st election" 2 "2nd election" 3 "{&ge} 3rd elections",replace

qui $xt_out i.proposal_periods_pre_closed_max3 i.closed_periods_max3 i.reversal_periods_max3 if $cond3
coefplot, xline(0) drop(1.proposal_periods_pre_closed_max3 2.proposal_periods_pre_closed_max3 _cons) levels(95 90) heading(1.closed_periods_max3="{bf:Decision to close}" 1.reversal_periods_max3="{bf:Proposal withdrawn}") grid(none) name("Figure3", replace)
graph export "$path_output/Figure3.eps", as(eps) name("Figure3") replace


// Table A1 //

// Different length on panels 
eststo clear
eststo: $xt_out t_post_t if $cond5
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond5
estadd local fixed "YES" , replace
esttab using "$path_output/TableA1.tex", prehead("\begin{table}[htbp]\centering \\  \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \\ \caption{Different minimum number of observations per precinct} \\ \begin{tabular}{l*{2}{c}} \hline\hline") posthead("\hline \\ \multicolumn{2}{c}{\textbf{Panel A: Only precincts unchanged between 2002-2018}} \\\\[-1ex]") fragment label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nocons nobase s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations"))
    eststo clear

eststo: $xt_out t_post_t if $cond1
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond1
estadd local fixed "YES" , replace

esttab using "$path_output/TableA1.tex", ///
   	posthead("\hline \\ \multicolumn{2}{c}{\textbf{Panel B: All precincts}} \\\\[-1ex]") ///
	fragment append prefoot("\hline") label se  b(3) se(3) varwidth(13) nogap star(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nocons nobase s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Panel A only includes precincts observed for all elections between 2002 and 2018. Panel B includes all precincts independent of the number of observations. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.001\)}\\ \end{tabular} \\ \label{tab:robust-panel-length} \\ \end{table}")
    eststo clear

// Table A2 // 
preserve
keep if consistent_party_panel_closed==1

eststo clear 
eststo: $xt_out t_post_t if $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/TableA2.tex",  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Exclude cases where the incumbent party has changed between proposal and closure") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase nocons s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-consistent-party-panel-closed} \end{table}")
    eststo clear

restore

// Table A3 // 
preserve
keep if consistent_party_panel_post_t==1
eststo clear 
eststo: $xt_out t_post_t if $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/TableA3.tex",  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) nocons varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Exclude cases where the incumbent party has changed after the proposal") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase nocons s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-consistent-party-panel-post-t} \end{table}")
    eststo clear

restore

// Table A4 //
preserve
drop if inlist(prop_year,2002,2006,2010,2014,2018)

eststo clear 
eststo: $xt_out t_post_t if $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/TableA4.tex",  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap nocons  replace star(* 0.10 ** 0.05 *** 0.01) title("Exclude proposals that occur during election years") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}")nonumbers nobase s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-not-election-year} \end{table}")
    eststo clear


restore

// Table A5 //
preserve
drop if multiple==1

eststo clear 
eststo: $xt_out t_post_t if $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/TableA5.tex",  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap nocons  replace star(* 0.10 ** 0.05 *** 0.01) title("Exclude precincts with multiple school closure at the same period") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-not-multiple-treat} \end{table}")
    eststo clear

restore

// Table A6 //
eststo: $xt_out t_post_t if $cond3_untreat
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond3_untreat
estadd local fixed "YES" , replace

esttab using "$path_output/TableA6.tex",  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Exclude precincts without any untreated periods") mtitles("\shortstack{(1)}" "\shortstack{(2)}") nonumbers nobase nocons s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-untreat-period} \end{table}")
    eststo clear
	
// Table A7	//
eststo clear
eststo: $xt_out t_post_t if inlist(incumbent_t, "S") & $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if inlist(incumbent_t, "S") &  $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/TableA7.tex", prehead("\begin{table}[htbp]\centering \\  \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \\ \caption{Separating effect for political parties} \\ \begin{tabular}{l*{2}{c}} \hline\hline") posthead("\hline \\ \multicolumn{2}{c}{\textbf{Panel A: Social Democrats}} \\\\[-1ex]") fragment label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap nocons replace star(* 0.10 ** 0.05 *** 0.01) mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations"))
    eststo clear

eststo: $xt_out t_post_t if inlist(incumbent_t, "M") & $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if inlist(incumbent_t, "M") &  $cond3
estadd local fixed "YES" , replace

esttab using "$path_output/TableA7.tex", ///
   	posthead("\hline \\ \multicolumn{2}{c}{\textbf{Panel B: Moderate Party}} \\\\[-1ex]") nomtitles ///
	fragment append prefoot("\hline") ///
	postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Panel A only includes cases where the Social democrats were incumbent at the election period of the proposal. Panel B only includes cases where the Moderate Party was incumbent at the election period of the proposal. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.001\)}\\ \end{tabular} \\ \label{tab:robust-party-analysis} \\ \end{table}") label se  b(3) se(3) varwidth(13) nogap nocons star(* 0.10 ** 0.05 *** 0.01) nonumbers nobase s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations"))
    eststo clear
	
// Table A8 // 
preserve
keep if max_pop_change<0.1
eststo clear
eststo: $xt_out t_post_t if  $cond3
estadd local fixed "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
esttab using "$path_output/TableA8.tex",  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Exclude precincts where the change number of eligible voters ever change by more then +/- 10 percent between two consecutive elections ") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase nocons s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-pop-change-10-percent} \end{table}")
    eststo clear
restore

// Table A9 // 
qui $xt_out post_t_pre_closed closed reversed_alt if $cond3
tab incumbent_t if t==1 & reversed_alt==0 & e(sample)
tab incumbent_t if t==1 & reversed_alt==1 & e(sample)

// Table B2 // 
gen time2=time*time

global lin_time c.time c.time#i.district_id
global quad_time c.time c.time2 c.time#i.district_id c.time#i.district_id

eststo: $xt_out $lin_time t_post_t if $cond3
estadd local fixed "YES" , replace
estadd local trend1 "YES" , replace
estadd local trend2 "NO" , replace

eststo: $xt_out $quad_time t_post_t if $cond3
estadd local fixed "YES" , replace
estadd local trend1 "YES" , replace
estadd local trend2 "YES" , replace

eststo: $xt_out $lin_time post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
estadd local trend1 "YES" , replace
estadd local trend2 "NO" , replace

eststo: $xt_out $quad_time post_t_pre_closed closed reversed_alt if $cond3
estadd local fixed "YES" , replace
estadd local trend1 "YES" , replace
estadd local trend2 "YES" , replace

esttab using "$path_output/TableB2.tex", keep(t_post_t post_t_pre_closed closed reversed_alt) order(t_post_t post_t_pre_closed closed reversed_alt) label se  b(3) se(3) obslast varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Precinct specific time trends") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed trend1 trend2 N_g N ,fmt(3 0 0) label("Precinct FE" "Precinct specific linear time trend" "Precinct specific quadratic time trend""\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{5}{p{\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-time-trends} \end{table}")
    eststo clear

// Table C1 //
eststo clear
eststo: xtreg diff_ink t_post_t if  N_observations>2 & anyt==1 &prop_year>2010, fe vce(cl district_id)
estadd local fixed "YES" , replace
eststo: xtreg diff_utb t_post_t if  N_observations>2 & anyt==1 &prop_year>2010, fe vce(cl district_id)
estadd local fixed "YES" , replace
eststo: xtreg diff_utr t_post_t if  N_observations>2 & anyt==1 &prop_year>2010, fe vce(cl district_id)
estadd local fixed "YES" , replace
esttab using "$path_output/TableC1.tex",  label se  b(3) se(3) order(t_post_t) varwidth(13) nogap  replace star(* 0.10 ** 0.05 *** 0.01) title("Effect of school closure proposals on demographic variables") mtitles("\shortstack{Income}" "\shortstack{Education}" "\shortstack{Foreign born}") nonumbers nobase nocons s(fixed N_g N ,fmt(3 0 0) label("Precinct FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.65\textwidth}}{\footnotesize \textit{Note}: Dependent variable: In the first column the difference in mean income between the precinct and the rest of the municipality is used. In the second and third column it is the difference in the share of inhabitants in the precinct with a degree from higher education (column 2) and who are foreign born (column 3), expressed as percentage points. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-background-info} \end{table}")
    eststo clear




// Table C2 // 
eststo clear 
eststo: $xt_out t_post_t if prop_year>2010 & year_left>=2010 & $cond3
estadd local fixed "YES" , replace
estadd local controls "NO" , replace
eststo: $xt_out t_post_t diff_ink diff_utb diff_utr if prop_year>2010 & year_left>=2010 & $cond3
estadd local fixed "YES" , replace
estadd local controls "YES" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt if prop_year>2010 & year_left>=2010 & $cond3
estadd local fixed "YES" , replace
estadd local controls "NO" , replace
eststo: $xt_out post_t_pre_closed closed reversed_alt diff_ink diff_utb diff_utr if prop_year>2010 & year_left>=2010 & $cond3
estadd local fixed "YES" , replace
estadd local controls "YES" , replace
esttab using "$path_output/TableC2.tex", nofloat nocons  label se  b(3) se(3) order(t_post_t post_t_pre_closed closed reversed_alt) drop(diff* _cons) varwidth(13) nogap replace star(* 0.10 ** 0.05 *** 0.01) title("Main results with controls") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed controls N_g N ,fmt(3 0 0) label("Precinct FE" "Control variables" "\# of precincts" "\# of observations")) postfoot("\hline\hline \end{tabular} \label{tab:main-fe-results-w-controls} \begin{tablenotes} \footnotesize \textit{Note}: Dependent variable: Difference in vote share for the party proposing to close a school in the precinct and the rest of the municipality, expressed in percentage points. The included control variables in columns (2) and (4) are measures of the difference in mean annual income, the share of highly educated, and the share of foreign-born in the precinct and the rest of the municipality. The sample is restricted to observations from 2010 and onwards and only includes precincts where the proposal was made after 2010. Standard errors are clustered per precinct in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\) \end{tablenotes} }")
    eststo clear

	
	
//// Load data for Appendix B ////

use "Data_AppendixB.dta", clear

// Preperation of the data //

// Genereate specific treatment indicators for different parties
// Loop over parties
foreach k in "C" "FP" "KD" "M" "MP" "S" "SD" "V" {
		local j =lower("`k'")
		gen t_post_t_`j'=0
		replace t_post_t_`j'=1 if t_post_t==1 & lower(incumbent_t)=="`j'"
		gen t_post_t_not_`j'=0
		replace t_post_t_not_`j'=1 if t_post_t==1 & t_post_t_`j'==0
		gen post_t_pre_closed_`j'=0
		replace post_t_pre_closed_`j'=1 if t_post_t_`j'==1 & any_closed==1 & closed==0
		gen post_t_pre_closed_not_`j'=0
		replace post_t_pre_closed_not_`j'=1 if t_post_t_not_`j'==1 & any_closed==1 & closed==0
		gen closed_`j'=0
		replace closed_`j'=1 if closed==1 & lower(incumbent_t)=="`j'"
		gen reversed_alt_`j'=0
		replace reversed_alt_`j'=1 if t_post_t_`j'==1 & reversed_alt==1

		replace voteshare_`j'=voteshare_`j'*100
		egen mun_treat_`j' = max(t_post_t_`j'), by(mun_code_left)
}

// Reshape the data to long format
reshape long votes_@ voteshare_@ t_post_t_@ t_post_t_not_@ post_t_pre_closed_@ post_t_pre_closed_not_@ closed_@ closed_not_@ reversed_alt_@  reversed_alt_not_@  , i(district_id year_left) j(party) string

// Create numeric indicator for party
encode party, gen(party_id)

// Create group variables for precinct+party, Muncipality+election year and municipality+party
egen district_party_id = group(district_id party_id)
egen mun_year = group(mun_code_left year_left)
egen mun_year_party = group(mun_year party_id)

// Set vote share to 0 if missing.
replace voteshare_=0 if voteshare_==.

// Label the main variables
label variable t_post_t_ "Proposal (all outcomes)"
label variable post_t_pre_closed_ "Proposal (before decision)"
label variable closed_ "Decision to close"
label variable reversed_alt_ "Proposal withdrawn"

// Table B1
eststo clear 
eststo: reghdfe voteshare t_post_t_ if length_panel>2,absorb(district_party_id mun_year_party) vce(cl district_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
eststo: reghdfe voteshare post_t_pre_closed_ closed_ reversed_alt_  if length_panel>2,absorb(district_party_id mun_year_party) vce(cl district_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
esttab using "$path_output/TableB1.tex",  label se  b(3) se(3) order(t_post_t_ post_t_pre_closed_ closed_ reversed_alt_) varwidth(13) nogap nocons replace star(* 0.10 ** 0.05 *** 0.01) title("Difference in Difference results, excluding precincts with fewer then 3 observations") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed mun_year N_clust N ,fmt(3 0 0) label("Precinct-by-party FE" "Party-by-municipality-by-election FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.9\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Vote shares for the parties expressed in percentage points. Standard errors clustered at the precinct-by-party level in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-did-all-parties-length3} \end{table}")
    eststo clear

	
	
//// Load Data for Appendix D ////
use "Data_AppendixD.dta", clear

// Preperation of the data //

// Genereate specific treatment indicators for different parties
// Loop over parties
foreach k in "C" "FP" "KD" "M" "MP" "S" "SD" "V" {
		local j =lower("`k'")
		gen t_post_t_`j'=0
		replace t_post_t_`j'=1 if t_post_t==1 & lower(incumbent_t)=="`j'"
		gen t_post_t_not_`j'=0
		replace t_post_t_not_`j'=1 if t_post_t==1 & t_post_t_`j'==0
		gen post_t_pre_closed_`j'=0
		replace post_t_pre_closed_`j'=1 if t_post_t_`j'==1 & any_closed==1 & closed==0
		gen post_t_pre_closed_not_`j'=0
		replace post_t_pre_closed_not_`j'=1 if t_post_t_not_`j'==1 & any_closed==1 & closed==0
		gen closed_`j'=0
		replace closed_`j'=1 if closed==1 & lower(incumbent_t)=="`j'"
		gen reversed_alt_`j'=0
		replace reversed_alt_`j'=1 if t_post_t_`j'==1 & reversed_alt==1


		egen mun_treat_`j' = max(t_post_t_`j'), by(mun_code_left)
}


// Reshape the data to long format
reshape long votes_@ voteshare_@ t_post_t_@ t_post_t_not_@ post_t_pre_closed_@ post_t_pre_closed_not_@ closed_@ closed_not_@ reversed_alt_@  reversed_alt_not_@ mun_treat_@ , i(vd_2018 year_left) j(party) string

// Create numeric indicator for party
encode party, gen(party_id)

// Generate indictor for observations where another party have made a proposal 
// in the same precincts
gen anyt_other= 0
replace anyt_other=1 if anyt==1 & party!=lower(incumbent_t)


// Create group variables for precinct+party, Muncipality+election year and municipality+party
egen vd_party_id = group(vd_2018 party_id)
egen mun_year = group(mun_code_left year_left)
egen mun_year_party = group(mun_year party_id)
replace voteshare_=0 if voteshare_==.

// Genereate indicators for number of observations since a) proposal, 
// b) proposal before closure, c) closure and d) withdrawl

// post proposal (indepdendept of outcome)
bysort vd_party_id (year_left): gen proposal_periods = min(sum(t_post_t_),3)
// post_t_pre_closed
bysort vd_party_id (year_left): gen post_t_pre_closed_periods = min(sum(post_t_pre_closed_),3)
replace post_t_pre_closed_periods=0 if post_t_pre_closed_!=1
// closed
bysort vd_party_id (year_left): gen closed_periods = min(sum(closed_),3)
// withdrawn
bysort vd_party_id (year_left): gen reversed_alt_periods = min(sum(reversed_alt_),3)

// Label the main variables
label variable t_post_t_ "Proposal (all outcomes)"
label variable post_t_pre_closed_ "Proposal (before decision)"
label variable closed_ "Decision to close"
label variable reversed_alt_ "Proposal withdrawn"

label define proposal_periods 1 "Proposal (all outcomes), first election" 2 "Proposal (all outcomes), second election" 3 "Proposal (all outcomes), third or later elections"
label define post_t_pre_closed_periods 1 "Proposal (before decision), first election" 2 "Proposal (before decision), second election" 3 "Proposal (before decision), third or later elections"
label define closed_periods 1 "Decision to close, first election" 2 "Decision to close, second election" 3 "Decision to close, third or later elections"
label define reversed_alt_periods 1 "Proposal withdrawn, first election" 2 "Proposal withdrawn, second election" 3 "Proposal withdrawn, third or later elections"

label values proposal_periods proposal_periods
label values post_t_pre_closed_periods post_t_pre_closed_periods
label values closed_periods closed_periods 
label values reversed_alt_periods reversed_alt_periods 


// Table D1 // 
eststo clear 
eststo: reghdfe voteshare t_post_t_ if anyt_other!=1,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
eststo: reghdfe voteshare post_t_pre_closed_ closed_ reversed_alt_ if anyt_other!=1,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
esttab using "$path_output/TableD1.tex",  label se  b(3) se(3) order(t_post_t_ post_t_pre_closed_ closed_ reversed_alt_) varwidth(13) nogap nocons replace star(* 0.10 ** 0.05 *** 0.01) title("Difference in Difference results, weighted sample") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed mun_year N_clust N ,fmt(3 0 0) label("Precinct-by-party FE" "Party-by-municipality-by-election FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.9\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Vote shares for the parties expressed in percentage points. Standard errors clustered at the precinct-by-party level in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-weighted-sample1} \end{table}")

// Table D2 //
eststo clear 
eststo: reghdfe voteshare i.proposal_periods if anyt_other!=1,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
eststo: reghdfe voteshare i.post_t_pre_closed_periods i.closed_periods i.reversed_alt_periods if anyt_other!=1,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
esttab using "$path_output/TableD2.tex",  label se  b(3) se(3) varwidth(13) nogap nocons replace star(* 0.10 ** 0.05 *** 0.01) title("Difference in Difference results, weighted sample, seperating effects over elections") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed mun_year N_clust N ,fmt(3 0 0) label("Precinct-by-party FE" "Party-by-municipality-by-election FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.9\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Vote shares for the parties expressed in percentage points. Standard errors clustered at the precinct-by-party level in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-weighted-sample2} \end{table}")



// Merge with buffer zons created in GeoPandas Python
merge m:1 vd_2018 using "Data_AppendixD2.dta" 
drop if _merge==2


// Generate indicator for neighboring precincts
// If two precincts that both are affected by a school closure proposals
// are neighbors, we include the precinct where the proposal happens first and
// exclude the other. If the proposals are made during the same election period
// both precincts are included 
gen n_pre = 0 
replace n_pre=1 if count_neighbors!=. & (first_year<election_year2 | election_year2==.)

// Table D3 //
eststo clear 
eststo: reghdfe voteshare t_post_t_ if anyt_other!=1 & n_pre==0 ,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
eststo: reghdfe voteshare post_t_pre_closed_ closed_ reversed_alt_ if anyt_other!=1 & n_pre==0 ,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
esttab using "$path_output/TableD3.tex",  label se  b(3) se(3) order(t_post_t_ post_t_pre_closed_ closed_ reversed_alt_) varwidth(13) nogap nocons replace star(* 0.10 ** 0.05 *** 0.01) title("Difference in Difference results, weighted sample, no neighboring precincts") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed mun_year N_clust N ,fmt(3 0 0) label("Precinct-by-party FE" "Party-by-municipality-by-election FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.9\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Vote shares for the parties expressed in percentage points. Standard errors clustered at the precinct-by-party level in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:robust-weighted-no-neighbor1} \end{table}")
eststo clear 

// Table D4 //
eststo clear 
eststo: reghdfe voteshare i.proposal_periods if anyt_other!=1 & n_pre==0 ,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
eststo: reghdfe voteshare i.post_t_pre_closed_periods i.closed_periods i.reversed_alt_periods if anyt_other!=1 & n_pre==0 ,absorb(vd_party_id mun_year_party) vce(cl vd_party_id)
	estadd local mun_year "YES" , replace
	estadd local fixed "YES" , replace
esttab using "$path_output/TableD4.tex",  label se  b(3) se(3) varwidth(13) nogap nocons replace star(* 0.10 ** 0.05 *** 0.01) title("Difference in Difference results, weighted sample,no neighboring precincts, seperating effects over elections") mtitles("\shortstack{(1)}" "\shortstack{(2) }" "\shortstack{(3)}" "\shortstack{(4)}") nonumbers nobase s(fixed mun_year N_clust N ,fmt(3 0 0) label("Precinct-by-party FE" "Party-by-municipality-by-election FE" "\# of precincts" "\# of observations")) postfoot("\hline\hline \multicolumn{3}{p{0.9\textwidth}}{\footnotesize \textit{Note}: Dependent variable: Vote shares for the parties expressed in percentage points. Standard errors clustered at the precinct-by-party level in parentheses. \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\ \end{tabular} \label{tab:weighted-sample-no-neighbor2} \end{table}")
eststo clear 
